/**
 * File name:      AbstractBaseAction.java
 * Date:           2013-3-18
 * Description:    // 用于详细说明此程序文件完成的主要功
 *                 // 能与其他模块或函数的接口，输出值、
 *                 // 取值范围、含义及参数间的关系
 * Modify History:     Date             Programmer       Notes
 *                    ---------        ---------------  ---------
 *                    2013-3-18            伍冲                              Initial
 **********************************************************************/
package com.wy.framework.actions;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.apache.struts2.util.TokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wy.business.market.pda.pojos.UsersRT;
import com.wy.framework.adapter.MessageSourceAdapter;
import com.wy.framework.constants.FieldConstant;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.pojos.TSysUserinfo;
import com.wy.framework.services.AbstractBaseService;
import com.wy.framework.services.login.LoginService;
import com.wy.component.redis.RedisUtil;

/**
 * Created on 2013-3-18
 * <p>Title:       [控制层基类]/p>
 * <p>Description: [控制层基类]</p>
 * @version        1.0
 */
public class AbstractBaseAction extends ActionSupport {

	private static final long serialVersionUID = -2702320468251928551L;

	@Autowired
	protected MessageSourceAdapter msgSourceAdapter;

	protected String rspCode = "000000";
	
	protected String rspMsg;
	
	protected String rspMemo;
	
	@Autowired
	protected LoginService loginService;
	
	/**
	 * 获取session信息			               
	*/
	public Object getSession(String key) {
		ActionContext ctx = ActionContext.getContext();
		return ctx.getSession().get(key);
	}
	
	public TSysUserinfo loginUser() throws Exception{
		String token = ServletActionContext.getRequest().getParameter("token");
		if(StringUtils.isEmpty(token))
			return null;
		String userId = RedisUtil.getUserId(token);
		TSysUserinfo t = loginService.getUserInfoDetail(userId);
		String deptName = RedisUtil.getUserId(token+"-dept");
		LOG.info("deptName : "+deptName);
		t.setDept(deptName);
		t.setDeptName(deptName);
		return t;
	}
	
	@JSON(name = "rspcode")
	public String getRspCode() {
		return rspCode;
	}
	
	@JSON(name = "rspmemo")
	public String getRspMemo() {
		return rspMemo;
	}

	@JSON(name = "rspmsg")
	public String getRspMsg() {
		if(rspMsg != null && rspMsg.length() > 0){
			return rspMsg;
		}
		if (rspCode.equals("000000")) {
			return msgSourceAdapter.getMessage(this.getClass().getSimpleName());
		} else {
			return msgSourceAdapter.getMessage(rspCode);
		}
	}
	
	/**
	 * 设置带占位符的响应信息				               
	*/
	public void setRspMsg(String rspCode, String ... parameter) {
		this.rspMsg = msgSourceAdapter.getMessage(rspCode, parameter);
	}
	
	public String getStrutsToken(){
		String token = (String)ActionContext.getContext().getSession().get(TokenHelper.DEFAULT_TOKEN_NAME);
		return token;
	}
	
}
